package com.fengmi.goods.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengmi.api.search.SearchApi;
import com.fengmi.entity.SpecAndOpts;
import com.fengmi.entity.goods.MallGoods;
import com.fengmi.entity.goods.MallGoodsBrand;
import com.fengmi.entity.goods.MallGoodsCat;
import com.fengmi.entity.goods.MallGoodsSku;
import com.fengmi.entity.vo.CatHotVo;
import com.fengmi.entity.vo.ResultVO;
import com.fengmi.goods.mapper.MallGoodsBrandMapper;
import com.fengmi.goods.mapper.MallGoodsCatMapper;
import com.fengmi.goods.mapper.MallGoodsMapper;
import com.fengmi.goods.mapper.MallGoodsSkuMapper;
import com.fengmi.goods.service.IMallGoodsService;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小马
 * @since 2021-10-12
 */
@Service
public class MallGoodsServiceImpl extends ServiceImpl<MallGoodsMapper, MallGoods> implements IMallGoodsService {

    @Autowired
    private SearchApi searchApi;
    @Autowired
    //查询品牌信息
    private MallGoodsBrandMapper goodsBrandMapper;
    @Autowired
    //查询商品分类信息
    private MallGoodsCatMapper goodsCatMapper;
    //查询sku列表
    @Autowired
    private MallGoodsSkuMapper goodsSkuMapper;

    //注入MQ模板
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public List<CatHotVo> findGoodsByCatId(Integer topn) {

        //获取商品分类
        List<MallGoodsCat> mallGoodsCats = this.baseMapper.selectHotCat();

        List<CatHotVo> catHotVos = mallGoodsCats.stream().map(cat -> {
            CatHotVo catHotVo = new CatHotVo();
            catHotVo.setMallGoodsCat(cat);

            //根据商品分类id获取列表信息
            List<MallGoods> goodsByCatId = this.baseMapper.findGoodsByCatId(String.valueOf(cat.getId()), topn+"");
            catHotVo.setGoodsList(goodsByCatId);
            return catHotVo;

        }).collect(Collectors.toList());


        return catHotVos;
    }

    @Override
    public List<MallGoods> selectGoodsSpuInfo() {
        return this.baseMapper.findGoodsSpuInfo();
    }

    @Override
    public ResultVO audit(String spuId) {
        //查询商品信息
        MallGoods mallGoods = this.baseMapper.selectById(spuId);


        if(mallGoods==null)
            return new ResultVO(false,"商品不存在");

        //查询品牌信息
        MallGoodsBrand mallGoodsBrand = goodsBrandMapper.selectById(mallGoods.getBrandId());
        mallGoods.setBrand(mallGoodsBrand);
        //查询商品分类信息
        MallGoodsCat mallGoodsCat = goodsCatMapper.selectById(mallGoods.getCategory3Id());
        mallGoods.setCat3(mallGoodsCat);
        MallGoodsCat mallGoodsCat2 = goodsCatMapper.selectById(mallGoods.getCategory2Id());
        mallGoods.setCat2(mallGoodsCat);
        MallGoodsCat mallGoodsCat1 = goodsCatMapper.selectById(mallGoods.getCategory1Id());
        mallGoods.setCat1(mallGoodsCat);

        mallGoods.setAuditStatus("1");
        this.baseMapper.updateById(mallGoods);

        //远程调用搜索服务

        //同步索引库
        SendResult sendResult = rocketMQTemplate.syncSend("goods-produce:goods-tags", mallGoods);
        if(sendResult.getSendStatus()!= SendStatus.SEND_OK){
            mallGoods.setAuditStatus("0");
            this.baseMapper.updateById(mallGoods);
            return new ResultVO(false,"审核失败:索引库同步失败");
        }

        //发送消息生成静态化页面
        SendResult resultHtml = rocketMQTemplate.syncSend("goodsToHtml:goods", mallGoods.getSpuId());
        if(resultHtml.getSendStatus()!=SendStatus.SEND_OK){
            mallGoods.setAuditStatus("0");
            this.baseMapper.updateById(mallGoods);
            return new ResultVO(false,"审核失败:索引库同步失败");
        }

        return new ResultVO(true,"审核成功");


//        try {
//            searchApi.goodsToBu(mallGoods);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            mallGoods.setAuditStatus("0");
//            this.baseMapper.updateById(mallGoods);
//        }

    }

    @Override
    public MallGoods findGoodsDetail(String spuId) {

        MallGoods mallGoods = this.baseMapper.selectById(spuId);

        //获取sku的列表
        QueryWrapper<MallGoodsSku> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id",spuId);
        queryWrapper.orderByDesc("is_default");
        List<MallGoodsSku> goodsSkuList = goodsSkuMapper.selectList(queryWrapper);
        mallGoods.setSkuList(goodsSkuList);

        List<SpecAndOpts> specAndOpts = JSONUtil.toList(mallGoods.getSpuSpecInfo(), SpecAndOpts.class);
        mallGoods.setSpecAndOpts(specAndOpts);

        return mallGoods;
    }
}
